package com.bwie.shop.controller;

import com.alibaba.fastjson.JSONObject;
import com.bwie.common.domain.Shop;
import com.bwie.common.result.Result;
import com.bwie.shop.service.ShopService;
import com.bwie.shop.utils.OssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
public class ShopController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private HttpServletRequest httpServletRequest;

    /**
     * 查询店铺合同数量
     * @return
     */
    @GetMapping("/selectShopQuantity")
    public Result<List<Shop>> selectShopQuantity(){
        log.info("功能:查询店铺合同数量");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        List<Shop> shops = shopService.selectShopQuantity();
        log.info("功能:查询店铺合同数量");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        log.info("响应结果:{}"+ JSONObject.toJSONString(shops));
        return Result.success(shops);
    }


    /**
     * 查询店铺
     * @return
     */
    @GetMapping("/queryShop")
    public Result<List<Shop>> queryShop(){
        log.info("功能:查询店铺");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        List<Shop> shops = shopService.queryShop();
        log.info("功能:查询店铺");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        log.info("响应结果:{}"+ JSONObject.toJSONString(shops));
        return Result.success(shops);
    }


    /**
     * 录入店铺
     * @param shop
     * @return
     */
    @PostMapping("/saveShop")
    public Result<Integer> saveShop(@RequestBody Shop shop){
        log.info("功能:录入店铺");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        log.info("请求参数:{}"+ JSONObject.toJSONString(shop));
        Integer integer = shopService.saveShop(shop);
        log.info("功能:录入店铺");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        log.info("响应结果:{}"+ JSONObject.toJSONString(integer));
        return Result.success(integer);
    }

    /**
     * 上传附件
     * @param file
     * @return
     */
    @PostMapping("/upLoad")
    public String upLoad(@RequestParam("file") MultipartFile file){
        String string = OssUtil.uploadMultipartFile(file);
        return string;
    }


    /**
     * 管理员审核
     * @param shop
     * @return
     */
    @PostMapping("/examine")
    public Result<Integer> examine(@RequestBody Shop shop){
        log.info("功能:管理员审核");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        log.info("请求参数:{}"+ JSONObject.toJSONString(shop));
        Integer examine = shopService.examine(shop);
        log.info("功能:管理员审核");
        log.info("请求方法:{}"+httpServletRequest.getMethod());
        log.info("请求路径:{}"+httpServletRequest.getRequestURL());
        log.info("响应结果:{}"+ JSONObject.toJSONString(examine));
        return Result.success(examine);
    }

    @GetMapping("/queryContractQuantity")
    public List<Shop> queryContractQuantity(){
        return shopService.queryContractQuantity();
    }
}
